package com.usian.behavior.listener;

import com.usian.behavior.service.IApFollowBehaviorService;
import com.usian.common.util.JsonUtils;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.Optional;

import static com.usian.common.constants.message.FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC;

//  关注行为，监听器
@Component
public class FollowBehaviorListener {

    @Autowired
    private IApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = FOLLOW_BEHAVIOR_TOPIC)
    public void receiveMsg(ConsumerRecord<?,?> record){
//        判断消息内容是否存在
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if (optional.isPresent()){
            //获取消息内容
            String value = (String) record.value();
            //解析消息内容
            FollowBehaviorDto followBehaviorDto = JsonUtils.toBean(value, FollowBehaviorDto.class);
            //记录关注行为
            apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);
        }
    }
}
